package net.zhengxinyun.performance.service.exam.answer;

import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.entity.AnswerEntity;
import net.zhengxinyun.performance.entity.UserEntity;
import net.zhengxinyun.performance.filter.AnswerFilter;
import net.zhengxinyun.performance.mapper.exam.answer.AnswerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
@Transactional
public class AnswerServiceImpl implements AnswerService {

    @Autowired
    private AnswerMapper mapper;

    @Override
    public List<AnswerFilter> query(AnswerFilter filter) {
        return null;
    }

    @Override
    public int queryForCount(AnswerFilter filter) {

        return  mapper.queryForCount(filter);
    }

    @Override
    public int save(List<AnswerEntity> list, UserEntity user) {

        for (AnswerEntity entity : list) {
            entity.setUserId(user.getUserPhone());
            entity.setAnswer(entity.getSelectId());
            //entity.setExamType("1");
            //测评对象中文和编码互转
            if (StringUtils.isNotBlank(entity.getExamer()))
            {
                List<String> strings = Util.strToList(entity.getExamer());
                String examer="";
                for (String string : strings) {
                    examer = Util.examerMap.get(string)+";"+examer;
                }
                entity.setExamer(examer);
            }
            int i= mapper.insert(entity);
            if(i==0)
            {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return  0;
            }
        }

        return 0;
    }

    @Override
    public int saveCache(AnswerEntity entity) {
        return mapper.insertCache(entity);
    }

    @Override
    public int update(AnswerEntity entity) {
        return 0;
    }

    @Override
    public int deleteByPrimaryKey(Long examId) {
        return 0;
    }

    @Override
    public int updateCache(AnswerEntity entity) {
        return mapper.updateCache(entity);
    }

    @Override
    public AnswerEntity selectCahche(AnswerEntity entity) {
        return mapper.selectCahche(entity);
    }
}
